package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.LbsTerminalReceiveLogRpcService;
import com.hunttown.mes.rpc.domain.LbsTerminalReceiveLogDTO;
import com.hunttown.mes.rpc.domain.query.LbsTerminalReceiveLogDTOQuery;
import com.hunttown.mes.service.LbsTerminalReceiveLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.LbsTerminalReceiveLogConverts.convert;

/**
 * created by wangjunfu on 2022-01-17 10:15:57
 */
@Service
public class LbsTerminalReceiveLogRpcServiceImpl implements LbsTerminalReceiveLogRpcService {

    private final Logger logger = LoggerFactory.getLogger(LbsTerminalReceiveLogRpcServiceImpl.class);

    private final LbsTerminalReceiveLogService lbsTerminalReceiveLogService;

    @Autowired
    public LbsTerminalReceiveLogRpcServiceImpl(LbsTerminalReceiveLogService lbsTerminalReceiveLogService) {
        this.lbsTerminalReceiveLogService = lbsTerminalReceiveLogService;
    }

    @Override
    public LbsTerminalReceiveLogDTO insert(LbsTerminalReceiveLogDTO lbsTerminalReceiveLogDTO) {
        try {
            return convert(lbsTerminalReceiveLogService.insert(convert(lbsTerminalReceiveLogDTO)));
        } catch (Exception e) {
            logger.error("LbsTerminalReceiveLog create failed.", e);
            throw new RuntimeException("LbsTerminalReceiveLog create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(LbsTerminalReceiveLogDTO lbsTerminalReceiveLogDTO) {
        try {
            return lbsTerminalReceiveLogService.updateInfoById(convert(lbsTerminalReceiveLogDTO));
        } catch (Exception e) {
            logger.error("LbsTerminalReceiveLog update by id failed.", e);
            throw new RuntimeException("LbsTerminalReceiveLog update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(LbsTerminalReceiveLogDTOQuery lbsTerminalReceiveLogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (lbsTerminalReceiveLogDTOQuery.getW_id() != null && lbsTerminalReceiveLogDTOQuery.getW_id() > 0) {
                query.put("w_id", lbsTerminalReceiveLogDTOQuery.getW_id());
            }

            if (lbsTerminalReceiveLogDTOQuery.getIds() != null && !lbsTerminalReceiveLogDTOQuery.getIds().equals("")) {
                query.put("ids", lbsTerminalReceiveLogDTOQuery.getIds());
            }

            if (lbsTerminalReceiveLogDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", lbsTerminalReceiveLogDTOQuery.getDeleteFlag());
            }

            return lbsTerminalReceiveLogService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("LbsTerminalReceiveLog update by query failed.", e);
            throw new RuntimeException("LbsTerminalReceiveLog update by query failed, Please order your code.", e);
        }
    }

    @Override
    public LbsTerminalReceiveLogDTO selectById(Integer id) {
        try {
            return convert(lbsTerminalReceiveLogService.selectById(id));
        } catch (Exception e) {
            logger.error("LbsTerminalReceiveLog select by id failed.", e);
            throw new RuntimeException("LbsTerminalReceiveLog select by id failed, Please order your code.", e);
        }
    }

    @Override
    public LbsTerminalReceiveLogDTO selectObjByQuery(LbsTerminalReceiveLogDTOQuery lbsTerminalReceiveLogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (lbsTerminalReceiveLogDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", lbsTerminalReceiveLogDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(lbsTerminalReceiveLogDTOQuery.getOrderBy())) {
                query.put("orderBy", lbsTerminalReceiveLogDTOQuery.getOrderBy());
            }

            return convert(lbsTerminalReceiveLogService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("LbsTerminalReceiveLog select obj by query failed.", e);
            throw new RuntimeException("LbsTerminalReceiveLog select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<LbsTerminalReceiveLogDTO> getListForPage(LbsTerminalReceiveLogDTOQuery lbsTerminalReceiveLogDTOQuery) {
        Query query = Query.page(lbsTerminalReceiveLogDTOQuery.getPn(), lbsTerminalReceiveLogDTOQuery.getPageSize());

        if (lbsTerminalReceiveLogDTOQuery.getId() != null && lbsTerminalReceiveLogDTOQuery.getId() > 0) {
            query.put("id", lbsTerminalReceiveLogDTOQuery.getId());
        }

        if (lbsTerminalReceiveLogDTOQuery.getIds() != null && !lbsTerminalReceiveLogDTOQuery.getIds().equals("")) {
            query.put("ids", lbsTerminalReceiveLogDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(lbsTerminalReceiveLogDTOQuery.getLikeName())) {
            query.put("likeName", lbsTerminalReceiveLogDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(lbsTerminalReceiveLogDTOQuery.getShowDate())) {
            query.put("showDate", lbsTerminalReceiveLogDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(lbsTerminalReceiveLogDTOQuery.getStartDate())) {
            query.put("startDate", lbsTerminalReceiveLogDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(lbsTerminalReceiveLogDTOQuery.getEndDate())) {
            query.put("endDate", lbsTerminalReceiveLogDTOQuery.getEndDate());
        }

        if (lbsTerminalReceiveLogDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", lbsTerminalReceiveLogDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(lbsTerminalReceiveLogDTOQuery.getOrderBy())) {
            query.put("orderBy", lbsTerminalReceiveLogDTOQuery.getOrderBy());
        }

        List<LbsTerminalReceiveLogDTO> list = convert(lbsTerminalReceiveLogService.selectByQuery(query));
        Page<LbsTerminalReceiveLogDTO> page = Page.from(lbsTerminalReceiveLogDTOQuery.getPn(), lbsTerminalReceiveLogDTOQuery.getPageSize(), list);
        page.setTotalItems(lbsTerminalReceiveLogService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return lbsTerminalReceiveLogService.deleteById(id);
        } catch (Exception e) {
            logger.error("LbsTerminalReceiveLog delete failed.", e);
            throw new RuntimeException("LbsTerminalReceiveLog delete failed, Please order your code.", e);
        }
    }
}